﻿/*
	Copyright 2009 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using Gloo.Core.Services;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Reflection;
using mshtml;
using System.Collections;

namespace Gloo.Services.Automation.Mshtml
{
	public class ServiceContainer : IServiceContainer<WebBrowser>, IDisposable
	{
		protected WebBrowser wbContainer;
		protected List<string> lPages;

		public Type tObjectType;

		public ServiceContainer(WebBrowser wbContainer)
		{
			this.wbContainer = wbContainer;

			wbContainer.Navigated += new WebBrowserNavigatedEventHandler(wbContainer_Navigated);
			wbContainer.Navigating += new WebBrowserNavigatingEventHandler(wbContainer_Navigating);
			wbContainer.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbContainer_DocumentCompleted);
			wbContainer.NewWindow += new System.ComponentModel.CancelEventHandler(wbContainer_NewWindow);

			lPages = new List<string>();
		}

		void wbContainer_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
		{
		}

		public void iterator(HtmlElementCollection hec)
		{
			Stack<IEnumerator> sStack = new Stack<IEnumerator>();

			sStack.Push(hec.GetEnumerator());

			while (sStack.Count > 0)
			{
			//     //div[@id='res']/div/ol/li 
				/*foreach (HtmlElement item in hec)
				{*/
				
				if (sStack.Peek().MoveNext())
				{
					HtmlElement item = (HtmlElement)sStack.Peek().Current;
					System.Console.WriteLine(item.TagName);
					sStack.Push(item.Children.GetEnumerator());
				}
				else
				{
					sStack.Pop();
				}
			}
		}

		void wbContainer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			ServiceProviderAttribute spServiceProvider = ((ServiceProviderAttribute[])tObjectType.GetCustomAttributes(typeof(ServiceProviderAttribute), true))[0];

			if (e.Url.OriginalString.Contains(spServiceProvider.ServiceContainer))
			{
				//Container.Document.
				iterator(Container.Document.Body.Children);
				IHTMLElementCollection hecElementCollection = ((IHTMLDocument2)Container.Document.DomDocument).all;
				IHTMLElement heFoundElement = null;

				object oObject = tObjectType.Assembly.CreateInstance(tObjectType.ToString());

				PropertyInfo[] pia = tObjectType.GetProperties();

				for (int i = 0; i < pia.Length; i++)
				{
					if (pia[i].GetCustomAttributes(typeof(ServiceParameterAttribute), true).Length > 0)
					{
						string sControl = ((ServiceParameterAttribute[])pia[i].GetCustomAttributes(typeof(ServiceParameterAttribute), true))[0].ParameterName;

						heFoundElement = (IHTMLElement)hecElementCollection.item(sControl, 0);

						if (heFoundElement != null)
						{
							pia[i].SetValue(oObject, ((HTMLInputElement)heFoundElement).value, null);
						}
					}
				}

				//create a class given the attributes in the properties
				//create a factory
				//create builder class
				//get the values for the properties
				//create the class
				//fill the properties or call the constructor
			}
		}

		void wbContainer_Navigating(object sender, WebBrowserNavigatingEventArgs e)
		{
		}

		void wbContainer_Navigated(object sender, WebBrowserNavigatedEventArgs e)
		{
		}

		public WebBrowser Container
		{
			get
			{
				return wbContainer;
			}
			set
			{
				wbContainer = value;
			}
		}

		public void Dispose()
		{
		}
	}
}
